//
// Created by luosong on 15/8/26.
// Copyright (c) 2015 CHANJET. All rights reserved.
//

#import "CSPTaskCompletionSource.h"
#import "CSPTask.h"


@interface CSPTaskCompletionSource ()

@property (nonatomic, strong, readwrite) CSPTask *task;

@end

@interface CSPTask (CSPTaskCompletionSource)

- (void)setResult:(id)result;
- (void)setError:(NSError *)error;
- (void)setException:(NSException *)exception;
- (void)cancel;
- (BOOL)trySetResult:(id)result;
- (BOOL)trySetError:(NSError *)error;
- (BOOL)trySetException:(NSException *)exception;
- (BOOL)trySetCancelled;

@end

@implementation CSPTaskCompletionSource

#pragma mark - Initializer

+ (instancetype)taskCompletionSource {
    return [[self alloc] init];
}

- (instancetype)init {
    if (self = [super init]) {
        _task = [[CSPTask alloc] init];
    }
    return self;
}

#pragma mark - Custom Setters/Getters

- (void)setResult:(id)result {
    [self.task setResult:result];
}

- (void)setError:(NSError *)error {
    [self.task setError:error];
}

- (void)setException:(NSException *)exception {
    [self.task setException:exception];
}

- (void)cancel {
    [self.task cancel];
}

- (BOOL)trySetResult:(id)result {
    return [self.task trySetResult:result];
}

- (BOOL)trySetError:(NSError *)error {
    return [self.task trySetError:error];
}

- (BOOL)trySetException:(NSException *)exception {
    return [self.task trySetException:exception];
}

- (BOOL)trySetCancelled {
    return [self.task trySetCancelled];
}

@end